package com.bjesc_b2c.app.appraiset.base;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.widget.Toast;

import com.orhanobut.logger.Logger;

/**
 * Created by Jason Chen on 2017/2/20.
 */

public class BaseFragmentActivity extends FragmentActivity {
    protected FragmentManager fragmentManager;
    protected Fragment indexfragment;
    protected Toast toast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        fragmentManager = getSupportFragmentManager();

    }

    /**
     * 切换Fragment
     */
    public void switchContent(Class<?> c,int layoutId) {
        if (indexfragment != null && indexfragment.getClass().getName().equals(c.getName())) {
            Logger.d("return");
            return;
        }
        Fragment fragmentTag = fragmentManager.findFragmentByTag(c.getName());
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (fragmentTag == null) {
            try {
                fragmentTag = (BaseFragment) c.newInstance();
                Logger.d("1");
                fragmentTransaction.add(layoutId, fragmentTag, c.getName());
                Logger.d("2");
            } catch (InstantiationException e) {
                e.printStackTrace();
                Logger.d("3");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                Logger.d("4");
            }
        } else {

            fragmentTransaction.show(fragmentTag);
            Logger.d("5");
        }
        if (indexfragment != null) {
            fragmentTransaction.hide(indexfragment);
            Logger.d("6");
        }
        indexfragment = fragmentTag;
        fragmentTransaction.commit();
        Logger.d("7");
    }
    /**
     * 切换Fragment
     */
    public void switchContent(Class<?> c,int layoutId,Bundle bundle) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (indexfragment != null && indexfragment.getClass().getName().equals(c.getName())) {
            return;
        }
        Fragment fragmentTag = fragmentManager.findFragmentByTag(c.getName());

        if (fragmentTag == null) {
            try {
                fragmentTag = (BaseFragment) c.newInstance();
                Logger.d("1");
                fragmentTag.setArguments(bundle);
                fragmentTransaction.add(layoutId, fragmentTag, c.getName());
                Logger.d("2");
            } catch (InstantiationException e) {
                e.printStackTrace();
                Logger.d("3");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                Logger.d("4");
            }
        } else {
            fragmentTransaction.show(fragmentTag);
            Logger.d("5");
        }
        if (indexfragment != null) {
            fragmentTransaction.hide(indexfragment);
            Logger.d("6");
        }
        indexfragment = fragmentTag;
        fragmentTransaction.commit();
        Logger.d("7");
    }

    protected void startIntent(Intent intent,boolean isFinish){
        startActivity(intent);
        if(isFinish){
            finish();
        }
    }

    /**
     *	//比较当前Fragment和点击Fragment是否同一个
     */
    public boolean equalsFragment(Class<?> c) {
        return indexfragment.getClass().getName().equals(c.getName());
    }

    protected void showToast(String text){
        if(toast==null){
            toast = Toast.makeText(this,text,Toast.LENGTH_SHORT);
        }else{
            toast.setText(text);
        }
        toast.show();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics() );
        return res;
    }
}
